<?php

/**
 * @name BdcController.php
 */
class BdcController extends AppController {
    # Controller name

    var $name = 'Bdc';
    # set the model to use
    var $uses = array('BdcDistribution','Depot','District','ProductType','Region','Waybill');
    # Set the layout to use
    var $layout = 'bdc_layout';

    public function beforeFilter() {
        parent::beforeFilter();
        $user_type = $this->Auth->user('user_type');
        if($user_type != 'bdc'){
            $this->redirect(array('controller'=>'Dashboard', 'action'=>'index'));
        }
    }

    function index() {
        
    }
    
    function enter_loading_data(){
        
    }
    
    function edit(){
        
    }
    
    function view(){
        
    }



    function demo() {
        
    }
    
    function enter_data(){
        
    }


    function daily_truck_upload($type = 'get'){
        $this->autoRender = false;
        $this->autoLayout = false;

        switch ($type) {
            case 'get' :
                $page = isset($_POST['page']) ? intval($_POST['page']) : 1;
                $limit = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
                $sort = isset($_POST['sort']) ? strval($_POST['sort']) : 'id';
                $order = isset($_POST['order']) ? strval($_POST['order']) : 'desc';
                $start = ($page-1)*$limit;
                $result = array();
                $items = array();

                $condition_array = array('BdcDistribution.deleted' => 'n');
               /* if (!empty($search_query)) {
                    if ($search_query == 'username') {
                        $condition_array = array(
                            'BdcDistribution.username' => $search_query,
                            'BdcDistribution.deleted' => 'n'
                        );
                    }
                    else {
                        $condition_array = array(
                            "BdcDistribution.$qtype LIKE" => $search_query . '%',
                            'BdcDistribution.deleted' => 'n'
                        );
                    }
                }*/

                $grid_count = $this->BdcDistribution->find('count', array('conditions'=>$condition_array,'recursive' => -1));
                $result["total"] = $grid_count;
                $grid_data = $this->BdcDistribution->find('all', array('conditions'=>$condition_array,'order' => "BdcDistribution.$sort $order", 'limit' => $start . ',' . $limit, 'recursive' => -1));

                foreach($grid_data as $data){
                    $items[] = $data['BdcDistribution'];
                }
                $result["rows"] = $items;

                echo json_encode($result);

            break;

            case 'save' :
                $data = array('BdcDistribution'=>$_POST);
                if ($this->BdcDistribution->save($data)){
                    echo json_encode(array('success'=>true));
                }
                else {
                    echo json_encode(array('msg'=>'Some errors occured.'));
                }
                //echo debug($data);

            break;

            case 'load':

            break;

            case 'delete':

            break;
        }

    }

}

?>